package com.glory.nicleanrpt

import com.glory.njcleanrpt.DataUtil
import com.glory.njcleanrpt.DateUtil
import com.glory.njcleanrpt.TranposUtils
import com.ivan.utils.PathUtil

/**
 * @Program: njcleanrpt
 * @Description: 在制品历史查询
 * @Author: ivan
 */
class WipHisQueryController {

    def dynamicQueryService

    /**
     * 在制品历史查询(一阶)
     * @return
     */
    def wipHisQuery() {
        request.setCharacterEncoding("UTF-8")
        response.setContentType("text/html;charset=UTF-8")
        params.startTime = DateUtil.getStartDate(params.startTime).startDate
        params.endTime = DateUtil.getEndDate(params.endTime).endDate
        def dateList = []
        def reportList = []
        def stepDescList = []
        def sysQueryPath = PathUtil.getPath(this, request)
        if ('Excel'.equals(params.EXPORT_CONTENT_NAME)) {
            params.SYS_QUERY_NAME = 'dateList'
            dateList = dynamicQueryService.queryForList(params)
            params.SYS_QUERY_NAME = 'wipHisQueryList'
            reportList = dynamicQueryService.queryForList(params)
            stepDescList = DataUtil.removeDuplicate(reportList.STEP_DESC)
            reportList = TranposUtils.colToRow('TRANS_TIME', 'MAIN_QTY', reportList)
        }
        [
                startTime     : params.startTime ?: '',
                endTime       : params.endTime ?: '',
                dateList      : dateList,
                stepDescList  : stepDescList,
                reportList    : reportList,
                SYS_QUERY_PATH: sysQueryPath
        ]
    }

    /**
     * 在制品历史查询(二阶)
     * @return
     */
    def wipHisQueryDetail() {
        request.setCharacterEncoding("UTF-8")
        response.setContentType("text/html;charset=UTF-8")
        params.stepDesc = URLDecoder.decode(params.stepDesc, 'UTF-8')
        params.startFlagTime = DateUtil.getStartDate(params.startFlagTime).startDate
        def reportList = []
        def tableHeadList = []
        def totalCount = 0
        def sysQueryName
        if (params.startTime == params.startFlagTime) {
            if ('Total'.equals(params.stepDesc)) {
                params.SYS_QUERY_NAME = 'wipHisQueryDetailCurrentTotalList'
                reportList = dynamicQueryService.queryForList(params)
                sysQueryName = params.SYS_QUERY_NAME
                if (reportList.size() > 0) {
                    tableHeadList = reportList[0].keySet()
                }
                totalCount = reportList.size()
            } else {
                params.SYS_QUERY_NAME = 'wipHisQueryDetailCurrentList'
                reportList = dynamicQueryService.queryForList(params)
                sysQueryName = params.SYS_QUERY_NAME
                if (reportList.size() > 0) {
                    tableHeadList = reportList[0].keySet()
                }
                totalCount = reportList.size()
            }
        }else {
            if ('Total'.equals(params.stepDesc)) {
                params.SYS_QUERY_NAME = 'wipHisQueryDetailOtherTotalList'
                reportList = dynamicQueryService.queryForList(params)
                sysQueryName = params.SYS_QUERY_NAME
                if (reportList.size() > 0) {
                    tableHeadList = reportList[0].keySet()
                }
                totalCount = reportList.size()
            } else {
                params.SYS_QUERY_NAME = 'wipHisQueryDetailOtherList'
                reportList = dynamicQueryService.queryForList(params)
                sysQueryName = params.SYS_QUERY_NAME
                if (reportList.size() > 0) {
                    tableHeadList = reportList[0].keySet()
                }
                totalCount = reportList.size()
            }
        }
        [
                startTime     : params.startTime ?: '',
                stepDesc     : params.stepDesc ?: '',
                reportList    : reportList,
                tableHeadList : tableHeadList,
                totalCount    : totalCount,
                SYS_QUERY_NAME: sysQueryName
        ]
    }

    def exportExcel() {
        params.startno = 1
        params.endno = 99999999999999999
        params.SYS_QUERY_NAME = 'dateList'
        def dateList = dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'wipHisQueryList'
        def reportList = dynamicQueryService.queryForList(params)
        reportList = TranposUtils.colToRow('TRANS_TIME', 'MAIN_QTY', reportList)
        String filename = new String("wipHisQueryList.xls".getBytes("UTF-8"), "UTF-8")
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(
                template: "export${params.EXPORT_CONTENT_NAME}",
                model: [
                        dateList  : dateList,
                        reportList: reportList
                ]
        )
    }
}